const express = require('express');
let pageRouter = express.Router();
let noteModel =require('../db/model/noteModel');
const verifyFn = require('../middlewares/verify');

pageRouter.get('/',(req,res)=>{
    res.redirect('/list');
})

pageRouter.get('/list',(req,res)=>{

    // 获取session里面是否有username
    let username = req.session.username;

    noteModel.find().then(data=>{
        // console.log(data);
        res.render('list.html',{data,username});
    })
})

pageRouter.get('/addnote',(req,res)=>{
    res.render('addnote.html');
})

pageRouter.get('/updatenote',verifyFn,(req,res)=>{
    let {_id} = req.query;

    noteModel.findById(_id).then(data=>{
        res.render('updatenote.html',{data});
    })
})

// 注册页面
pageRouter.get('/register',(req,res)=>{
    res.render('register.html');
})

// 登录页面
pageRouter.get('/login',(req,res)=>{
    res.render('login.html');
})


module.exports = pageRouter;